package com.prs.crm.dao.cqunicom.impl;


import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cqunicom.CompanyDao;
import com.prs.crm.dao.impl.BaseDaoImpl;

import com.prs.crm.domain.cqunicom.Company;
/**
 * @author  WangTing
 *
 * 创建时间：2011-3-4 
 * 
 */

public class CompanyDaoImpl extends BaseDaoImpl<Company> implements CompanyDao {

	@SuppressWarnings("unchecked")

	@Override
	public Collection<Company> getAllCompany() {
		// TODO Auto-generated method stub
		return (Collection<Company>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Company.class);
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Company> getCompanyBySerialNumber(final String serial_number) {
		// TODO Auto-generated method stub
		return (Collection<Company>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Company.class);
				criteria.add(Restrictions.eq("serial_number", serial_number));
				
				 //System.out.println(criteria.list().size()+"size");
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Company getCompanyBySerialNum(final String serial_number) {
		// TODO Auto-generated method stub
		return (Company)  this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Company.class);
				criteria.add(Restrictions.eq("serial_number", serial_number));
			
				List<Company> result = (List<Company>) criteria.list();

		   if (result.size() > 0){
						return result.get(0);
			}
		               return null;
			
			}
			
		});
	}

	@Override
	public Company getCompany(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, id);
	}

	@Override
	public Company getByName( final String name) {
		return (Company)  this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Company.class);
				criteria.add(Restrictions.eq("comName", name));
			
				List<Company> result = (List<Company>) criteria.list();

		   if (result.size() > 0){
						return result.get(0);
			}
		               return null;
			
			}
			
		});
	}


}
